using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Splino
{
    public partial class UndoRedoMultiple : Form
    {
        /// <summary>
        /// Gets or sets the undo/redo Button
        /// </summary>
        public Button UndoRedoButton
        {
            get { return _button; }
            set { _button = value; }
        }

        /// <summary>
        /// Gets or sets the ListView for displaying the undo/redo commands
        /// </summary>
        public ListView ListView
        {
            get { return _listView; }
            set { _listView = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public UndoRedoMultiple()
        {
            InitializeComponent();
        }

        private void _listView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                for (int i = 0; i < e.ItemIndex; i++)
                {
                    _listView.Items[i].BackColor = SystemColors.Highlight;
                    _listView.Items[i].ForeColor = SystemColors.HighlightText;
                }
            }
            else
                for (int i = 0; i < _listView.Items.Count; i++)
                {
                    _listView.Items[i].BackColor = SystemColors.Window;
                    _listView.Items[i].ForeColor = SystemColors.WindowText;
                }
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void _button_Click(object sender, EventArgs e)
        {
            int selectedIndex;
            if (_listView.SelectedIndices.Count > 0)
            {
                selectedIndex = _listView.SelectedIndices[0];

                if (_button.Text == "Undo")
                    for (int i = 0; i <= selectedIndex; i++)
                        CommandManager.Instance.ExecuteCommand(new Commands.Undo());
                else
                    for (int i = 0; i <= selectedIndex; i++)
                        CommandManager.Instance.ExecuteCommand(new Commands.Redo());

                Close();
            }
            else
                MessageBox.Show(
                    "Please select items before clicking " + _button.Text + ".",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
        }
    }
}